package com.dd.cloud.user.service.aicms.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.common.exception.ZekeException;
import com.dd.cloud.common.utils.ExtUtils;
import com.dd.cloud.user.config.ZerkProperties;
import com.dd.cloud.user.entity.aicms.AicmsSymptom;
import com.dd.cloud.user.entity.aicms.AicmsSymptomExplain;
import com.dd.cloud.user.mapper.aicms.AicmsSymptomMapper;
import com.dd.cloud.user.req.aicms.AicmsSymptomAIMarkReq;
import com.dd.cloud.user.service.aicms.IAicmsSymptomExplainService;
import com.dd.cloud.user.service.aicms.IAicmsSymptomService;
import com.dd.cloud.user.vo.SymptomVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author czg
 * @since 2022-04-27
 */
@Service
@Slf4j
public class AicmsSymptomServiceImpl extends ServiceImpl<AicmsSymptomMapper, AicmsSymptom> implements IAicmsSymptomService {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private ZerkProperties zerkProperties;
    @Autowired
    private IAicmsSymptomExplainService aicmsSymptomExplainService;

    /**
     * 通过序号查询症状 以及症状解释
     *
     * @param xuHao 序号
     * @return
     */
    @Override
    public List<AicmsSymptom> querySymptoms(String xuHao) {
        QueryWrapper<AicmsSymptom> qs = new QueryWrapper<>();
        qs.lambda().eq(AicmsSymptom::getPXuhao, xuHao);
        List<AicmsSymptom> list = list(qs);
        if (list == null || list.isEmpty()) {
            String str = restTemplate.getForObject(zerkProperties.getSymptomsUrl() + xuHao, String.class);
            SymptomVo vo = JSON.parseObject(str, SymptomVo.class);
            log.info("根据疾病序号获取症状 参数xuhao=：" + xuHao);
            log.info("根据疾病序号获取症状结果：" + vo);
            if (vo != null && vo.getnErrorCode() == 0) {
                list = vo.getZhengzhuang();
            }
        }
        List<AicmsSymptomExplain> list2 = aicmsSymptomExplainService.list();
        for (AicmsSymptom item1 : list) {
            for (AicmsSymptomExplain item2 : list2) {
                if (item1.getSName().contains(item2.getSName())) {
                    item1.setSExplain(item2.getSName() + ":" + item2.getSExplain());
                    break;
                }
            }
        }
        return list;
    }

    @Override
    public List<AicmsSymptom> querySymptomsKeyword(String keyword,String xuHao) {
        if (StringUtils.isBlank(keyword)) {
            return new ArrayList<>();
        }
        List<AicmsSymptom> aicmsSymptoms = this.getBaseMapper().querySymptomsKeyword(keyword,xuHao);
        if(CollectionUtil.isEmpty(aicmsSymptoms)){
            return new ArrayList<>();
        }
        List<AicmsSymptomExplain> list2 = aicmsSymptomExplainService.list();
        for (AicmsSymptom item1 : aicmsSymptoms) {
            for (AicmsSymptomExplain item2 : list2) {
                if (item1.getSName().contains(item2.getSName())) {
                    item1.setSExplain(item2.getSName() + ":" + item2.getSExplain());
                    break;
                }
            }
        }
        return aicmsSymptoms;
    }

    /**
     * @description 导入症状口语化表达ai的标志位
     * @param file:
     * @return void
     * @author czg
     * @date 2024/2/2 17:34
    */
    @Override
    public void symptomImportAi(MultipartFile file) {
        if (file == null) {
            throw new ZekeException(400, "请上传文件");
        }
        if (!file.getOriginalFilename().contains(".xlsx") && !file.getOriginalFilename().contains(".xls")) {
            throw new ZekeException(400, "只支持xlsx和xls文件格式");
        }
        List<AicmsSymptomAIMarkReq> list = new ArrayList<>();
        try {
            list = ExtUtils.importExcel(file, 0, 1, AicmsSymptomAIMarkReq.class);
        } catch (IOException e) {
            e.printStackTrace();
            throw new ZekeException(400, "解析Excel失败");
        }

        if (list == null || list.isEmpty()) {
            throw new ZekeException(400, "文档数据为空");
        }
        list.forEach(item ->{
            if (StringUtils.isNotBlank(item.getName())){
                QueryWrapper<AicmsSymptom> qs = new QueryWrapper<>();
                qs.lambda().eq(AicmsSymptom::getSName, item.getName());
                List<AicmsSymptom> symptoms = list(qs);
                symptoms.stream().forEach(s -> s.setAiFlag(item.getMark()));
                updateBatchById(symptoms);
            }
        });
    }
}
